package com.leyou.page.Service.impl;

import com.leyou.api.controller.FeignController;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.JsonUtils;
import com.leyou.page.Service.PageService;
import com.leyou.page.Vo.*;
import com.leyou.pojo.dto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PageServiceImpl implements PageService {
    @Autowired
    private FeignController feignController;
    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String SPU_KEY_PREFIX ="ly:page:spu:id:";
    private static final String SKU_KEY_PREFIX ="ly:page:sku:id:";
    private static final String DETAIL_KEY_PREFIX ="ly:page:detail:id:";
    private static final String BRAND_KEY_PREFIX ="ly:page:brand:id:";
    private static final String CATEGORY_KEY_PREFIX ="ly:page:category:id:";
    private static final String SPEC_KEY_PREFIX ="ly:page:spec:id:";

    @Override
    public String querySpuById(Long spuId) {
        //查询spu
        SpuDto spuDto = feignController.querySpu(spuId);
        //判断商品是否为空
        if(spuDto==null){
            throw new LyException("商品不存在",400);
        }
        //转换成vo
        SpuVO spuVO = spuDto.toEntity(SpuVO.class);
        //注入categorys的值
        spuVO.setCategoryIds(spuDto.getCategoryIds());
        //转换成json，放入redis
        String s = JsonUtils.toJson(spuVO);
        redisTemplate.opsForValue().set(SPU_KEY_PREFIX+spuId,s);
        return s;
    }

    @Override
    public String querySkuById(Long spuId) {
        //查询sku
        List<SkuDto> skuDtoList = feignController.querySkuBySpuId(spuId);
        if(skuDtoList==null){
            throw new LyException("商品不存在",400);
        }
        //转换成json，放入redis
        String s = JsonUtils.toJson(skuDtoList);
        redisTemplate.opsForValue().set(SKU_KEY_PREFIX+spuId,s);
        return s;
    }

    @Override
    public String queryDetailById(Long spuId) {
        //查询spuDetail
        SpuDetailsDto spuDetailsDto = feignController.querySpuDetail(spuId);
        if(spuDetailsDto==null){
            throw new LyException("商品不存在",400);
        }
        //转换成json，放入redis
        String s = JsonUtils.toJson(spuDetailsDto);
        redisTemplate.opsForValue().set(DETAIL_KEY_PREFIX+spuId,s);
        return s;
    }

    @Override
    public String queryBrandById(Long brandId) {
//        //查询spu
//        SpuDto spuDto = feignController.querySkuAndSpuAndSpuDetail(spuId);
//        //设置brandVo
//        BrandVO brandVO = new BrandVO();
//        brandVO.setId(spuDto.getBrandId());
//        brandVO.setName(spuDto.getBrandName());
        //查询brandDto
        BrandDto brandDto = feignController.queryBrandById(brandId);
        if(brandDto==null){
            throw new LyException("商品不存在",400);
        }
        //转换dto
        BrandVO brandVO = brandDto.toEntity(BrandVO.class);
        //转换成json，放入redis
        String s = JsonUtils.toJson(brandVO);
        redisTemplate.opsForValue().set(BRAND_KEY_PREFIX+brandId,s);

        return s;
    }

    @Override
    public String queryCategoryById(List<Long> categoryIds) {

//        //查询spu
//        SpuDto spuDto = feignController.querySkuAndSpuAndSpuDetail(spuId);
//        CategoryVO categoryVO = new CategoryVO();
//        if(spuDto.getCategoryIds().size()!=3){
//            throw new LyExceptions("请求参数格式错误",400);
//        }
//        categoryVO.setId(spuDto.getCategoryIds().get(2));
//        categoryVO.setName(spuDto.getCategoryName());
        if(categoryIds.size()!=3){
            throw new LyException("参数格式错误",400);
        }
        List<CategoryDto> list = feignController.queryCategoriesByIds(categoryIds);
        if(list==null){
            throw new LyException("商品不存在",400);
        }
        //装换vo
        List<CategoryVO> voList = list.stream().map(categoryDto -> categoryDto.toEntity(CategoryVO.class)).collect(Collectors.toList());
        //转换成json，放入redis
        String s = JsonUtils.toJson(voList);
        redisTemplate.opsForValue().set(CATEGORY_KEY_PREFIX+categoryIds.get(2),s);
        return s;
    }

    @Override
    public String queryGroupAndParamById(Long categoryId) {
//        //查询category的id
//        SpuDto spu = feignController.querySpu(spuId);
//        Long aLong = spu.getCategoryIds().get(2);
        //查询规格参数组查询规格参数
        List<SpecGroupDto> specGroupDtos = feignController.querySpecGroupAndParamById(categoryId);
        if(specGroupDtos==null){
            throw new LyException("商品不存在",400);
        }
        ArrayList<SpecGroupVO> list = new ArrayList<>();
        for (SpecGroupDto specGroupDto : specGroupDtos) {
            SpecGroupVO specGroupVO = new SpecGroupVO();
            List<SpecParamVO> specParamVOS = specGroupDto.getList().stream()
                    .map(specParamDto -> specParamDto.toEntity(SpecParamVO.class)).collect(Collectors.toList());
            specGroupVO.setName(specGroupDto.getName());
            specGroupVO.setParams(specParamVOS);
            list.add(specGroupVO);
        }
        //转换成json，放入redis
        String s = JsonUtils.toJson(list);
        redisTemplate.opsForValue().set(SPEC_KEY_PREFIX+categoryId,s);
        return s;
    }

    @Override
    public void deleteSkuFromRedis(Long id, Long spuId) {
        //从redis中删出
        redisTemplate.delete(SKU_KEY_PREFIX+spuId);
    }
}
